//
//  ViewController.m
//  AppleLoginDemo
//
//  Created by 郭桥生 on 2019/12/17.
//  Copyright © 2019 TLX. All rights reserved.
//

#import "ViewController.h"
#import <AuthenticationServices/AuthenticationServices.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:1 alpha:0.8];
    
    //统一的宽高以及圆角 0 或者按钮高度一半
    CGSize size0 = CGSizeMake(260, 50);
    CGFloat cornerRadius = size0.height / 2.0f;
    CGFloat plusValue = (size0.height + 10) * 3;
    
    //第一组的登录图标 ASAuthorizationAppleIDButtonTypeSignIn
    NSArray *array0 = @[@(ASAuthorizationAppleIDButtonStyleWhite),
                        @(ASAuthorizationAppleIDButtonStyleBlack),
                        @(ASAuthorizationAppleIDButtonStyleWhiteOutline)];
    for (int i = 0; i < array0.count; i++) {
        NSInteger style = [array0[i] integerValue];
        ASAuthorizationAppleIDButton *button = [[ASAuthorizationAppleIDButton alloc] initWithAuthorizationButtonType:ASAuthorizationAppleIDButtonTypeSignIn authorizationButtonStyle:style];
        [button setFrame:CGRectMake(20, 30 + ((size0.height + 10) * i) , size0.width, size0.height)];
        [button setCornerRadius:cornerRadius];
        [self.view addSubview:button];
    }
    
    //第二组的登录图标 ASAuthorizationAppleIDButtonTypeSignIn
    for (int i = 0; i < array0.count; i++) {
        NSInteger style = [array0[i] integerValue];
        ASAuthorizationAppleIDButton *button = [[ASAuthorizationAppleIDButton alloc] initWithAuthorizationButtonType:ASAuthorizationAppleIDButtonTypeSignUp authorizationButtonStyle:style];
        [button setFrame:CGRectMake(20, plusValue + 30 + ((size0.height + 10) * i) , size0.width, size0.height)];
        [button setCornerRadius:cornerRadius];
        [self.view addSubview:button];
    }
    
    //第三组的登录图标 ASAuthorizationAppleIDButtonTypeContinue
    for (int i = 0; i < array0.count; i++) {
        NSInteger style = [array0[i] integerValue];
        ASAuthorizationAppleIDButton *button = [[ASAuthorizationAppleIDButton alloc] initWithAuthorizationButtonType:ASAuthorizationAppleIDButtonTypeContinue authorizationButtonStyle:style];
        [button setFrame:CGRectMake(20, plusValue * 2 + 30 + ((size0.height + 10) * i) , size0.width, size0.height)];
        [button setCornerRadius:cornerRadius];
        [self.view addSubview:button];
    }
    
    //第四组的登录图标 方形
    CGSize size1 = CGSizeMake(40, 40);
    CGFloat cornerRadius1 = 10.0f;
    //CGFloat cornerRadius1 = size1.height / 2.0f;
    for (int i = 0; i < array0.count; i++) {
        NSInteger style = [array0[i] integerValue];
        ASAuthorizationAppleIDButton *button = [[ASAuthorizationAppleIDButton alloc] initWithAuthorizationButtonType:ASAuthorizationAppleIDButtonTypeContinue authorizationButtonStyle:style];
        [button setFrame:CGRectMake(330, 30 + ((size1.height + 10) * i) , size1.width, size1.height)];
        [button setCornerRadius:cornerRadius1];
        [self.view addSubview:button];
    }
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setFrame:CGRectMake(50, self.view.frame.size.height - 60, 200, 50)];
    [button setTitle:@"截屏" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)buttonAction {
    NSArray *subViews = self.view.subviews;
    for (UIView *subView in subViews) {
        if ([subView isKindOfClass:ASAuthorizationAppleIDButton.class]) {
            UIImage *image = [self captureScreenForView:subView];
            UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        }
    }
}

- (UIImage *)captureScreenForView:(UIView *)currentView {
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(currentView.frame.size.width, currentView.frame.size.height), NO, 0.0);//原图
    [currentView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewImage;
}

//保存图片的结果
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        NSLog(@"save error %@", error);
    }
}

@end
